package cn.tedu.loding.controller;

import cn.tedu.loding.pojo.dto.OrderAddNewDTO;
import cn.tedu.loding.pojo.vo.OrderListItemVO;
import cn.tedu.loding.pojo.vo.OrderStandardVO;
import cn.tedu.loding.service.IOrderService;
import cn.tedu.loding.web.JsonResult;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理订单相关请求的控制器
 *
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/orders")
@Api(tags = "10. 订单管理模块")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    public OrderController() {
        log.debug("创建控制器对象：OrderController");
    }

    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid OrderAddNewDTO OrderAddNewDTO) {
        log.debug("开始处理【添加订单】的请求，参数：{}", OrderAddNewDTO);
        orderService.addNew(OrderAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id) {
        log.debug("开始处理【根据id删除订单】的请求，参数：{}", id);
        orderService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询订单详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<OrderStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询订单详情】的请求，参数：{}", id);
        OrderStandardVO order = orderService.getStandardById(id);
        return JsonResult.ok(order);
    }

    @ApiOperation("订单列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")
    public JsonResult<List<OrderListItemVO>> list(){
        log.debug("开始处理【订单列表】请求");
        List<OrderListItemVO> list = orderService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("指定用户订单列表")
    @ApiOperationSupport(order = 411)
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long")
    @GetMapping("/{userId:[0-9]+}/list")
    public JsonResult<List<OrderListItemVO>> listByUserId(@PathVariable Long userId){
        log.debug("开始处理【指定用户订单列表】请求");
        List<OrderListItemVO> list = orderService.listByUserId(userId);
        return JsonResult.ok(list);
    }

    @ApiOperation("分页查询的订单列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/{page}/page/{pageSize}/pageSize")
    public JsonResult<PageInfo<OrderListItemVO>> orderList(@PathVariable Integer page, @PathVariable Integer pageSize){
        log.debug("开始处理【分页查询的订单列表】请求,参数为{},{}",page,pageSize);
        PageInfo<OrderListItemVO> pageInfo = orderService.getAllOrderByPage(page,pageSize);
        return JsonResult.ok(pageInfo);
    }
}
